using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MediatR;
using RBAC.ErrorCode;
using RBAC.Write.Api.Application.Command.ProcessManagements;
using RBAC.Domain.ProcessManagement;
using System.IO;

namespace RBAC.Write.Api.Controllers.ProcessManagements
{

    /// <summary>
    ///工序管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ProcessController : ControllerBase
    {

        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">中介者</param>
        public ProcessController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 新增工序分类
        /// </summary>
        /// <param name="command">工序分类信息</param>
        /// <returns>新增结果</returns>
        [HttpPost]
        public async Task<APIResult<int>> AddProcessClassification([FromBody] AddProcessClassificationCommand command)
        {
            return await mediator.Send(command);
        }

        /// <summary>
        /// 编辑工序分类
        /// </summary>
        /// <param name="command">工序分类信息</param>
        /// <returns>编辑结果</returns>
        [HttpPut]
        public async Task<APIResult<int>> UpdateProcessClassification([FromBody] UpdateProcessClassificationCommand command)
        {
            return await mediator.Send(command);
        }

        /// <summary>
        /// 批量删除工序分类（逻辑删除）
        /// </summary>
        /// <param name="command">ID集合</param>
        /// <returns>删除条数</returns>
        [HttpPost]
        public async Task<APIResult<int>> BatchDeleteProcessClassifications([FromBody] BatchDeleteProcessClassificationsCommand command)
        {
            return await mediator.Send(command);
        }

        /// <summary>
        /// 新增工序（主表+附件）
        /// </summary>
        /// <param name="command">工序信息</param>
        /// <returns>新增结果（新工序ID）</returns>
        [HttpPost]
        public async Task<APIResult<int>> AddProcess([FromBody] AddProcessCommand command)
        {
            return await mediator.Send(command);
        }

        /// <summary>
        /// 编辑工序（主表+附件全量覆盖）
        /// </summary>
        /// <param name="command">工序信息</param>
        /// <returns>编辑结果</returns>
        [HttpPut]
        public async Task<APIResult<int>> UpdateProcess([FromBody] UpdateProcessCommand command)
        {
            return await mediator.Send(command);
        }

        /// <summary>
        /// 批量删除工序（逻辑删除）
        /// </summary>
        /// <param name="command">ID集合</param>
        /// <returns>删除条数</returns>
        [HttpPost]
        public async Task<APIResult<int>> BatchDeleteProcesses([FromBody] BatchDeleteProcessesCommand command)
        {
            return await mediator.Send(command);
        }

        /// <summary>
        /// 上传工序附件（任意格式，无需表单ID）
        /// </summary>
        [HttpPost]
        [Consumes("multipart/form-data")]
        public async Task<APIResult<string>> UploadProcessAttachment([FromForm] UploadProcessAttachmentForm form)
        {
            if (form == null || form.File == null || form.File.Length == 0)
            {
                return new APIResult<string> { Code = APIEnums.Fail, Msg = "文件为空", Data = string.Empty };
            }

            await using var ms = new MemoryStream();
            await form.File.CopyToAsync(ms);
            var cmd = new UploadProcessAttachmentCommand
            {
                FileName = form.File.FileName,
                FileBytes = ms.ToArray(),
                ContentType = form.File.ContentType
            };
            return await mediator.Send(cmd);
        }

    }
}
